'''
Created on Mar 16, 2012

@author: dolphinigle
'''

from Tkinter import *
import Tkinter
from controller import base

class GUI:
  def __init__(self, master):
    self.master = master
    frame = Frame(master)
    frame.grid(column=0, row=0, sticky=(N, W, E, S))
    frame.rowconfigure(0, weight=1)
    frame.columnconfigure(0, weight=1)
    frame.pack()

    self.sigimg = PhotoImage(file="sig.gif")
    self.sepimg = PhotoImage(file="separate.gif")
    self.corimg = PhotoImage(file="correct.gif")
    self.wroimg = PhotoImage(file="wrong.gif")
    self.thiimg = PhotoImage(file="thinking.gif")
    self.donimg = PhotoImage(file="yaysig.gif")

    self.logo = Label(frame, image=self.sigimg)
    self.logo.grid(row=1, column=2, sticky=(W))

    Label(frame, text='File:').grid(row=2, column=1, sticky=(W))
    Label(frame, text='Autograph File:').grid(row=3, column=1, sticky=(W))
    Label(frame, text="SNSD's Generated Autograph:").grid(row=4, column=1, sticky=(W))
    Label(frame, text='Your SNSD Girl:').grid(row=5, column=1, sticky=(W))

    Button(frame, text='Ask SNSD for Autograph', command=self.sign_clicked).grid(
      row=6, column=1, sticky=(W))
    Button(frame, text='Is your autograph SNSD authentic?', command=self.verify_clicked).grid(
      row=6, column=2, sticky=(W))

    Button(frame, text='Next Girl', command=self.ganti).grid(
      row=5, column=3, sticky=(W))

    self.input_txt = StringVar()
    self.input_txt.set('../testdata/test.in');
    self.autograph_txt = StringVar()
    self.autograph_txt.set('../testdata/test.auto')
    self.gen_txt = StringVar()
    self.mode = StringVar()
    self.mode.set('TAEYEON - In The Same File')

    Entry(frame, width=50, textvariable=self.input_txt).grid(
      row=2, column=2, sticky=(W))
    Entry(frame, width=50, textvariable=self.autograph_txt).grid(
      row=3, column=2, sticky=(W))
    Entry(frame, width=50, textvariable=self.gen_txt).grid(
      row=4, column=2, sticky=(W))
    Label(frame, width=50, textvariable=self.mode).grid(
      row=5, column=2, sticky=(W))
    
    Label(frame, text='File:').grid(row=2, column=1, sticky=(W))
    Label(frame, text='Autograph File:').grid(row=3, column=1, sticky=(W))
    
    Label(frame, text='p:').grid(row=7, column=1, sticky=(W))
    Label(frame, text='g:').grid(row=8, column=1, sticky=(W))
    Label(frame, text='x:').grid(row=9, column=1, sticky=(W))
    Label(frame, text='y:').grid(row=10, column=1, sticky=(W))
    
    self.p = StringVar()
    self.p.set('806009383965750840509539765019699247480325202797007');
    self.g = StringVar()
    self.g.set('428651859920367310278363197758101172448797548205046')
    self.x = StringVar()
    self.x.set('114504280771065305715113334844301290451203747894179')
    self.y = StringVar()
    self.y.set('190084575433011681096488022565959735860656319540369')
    
    Entry(frame, width=50, textvariable=self.p).grid(
      row=7, column=2, sticky=(W))
    Entry(frame, width=50, textvariable=self.g).grid(
      row=8, column=2, sticky=(W))
    Entry(frame, width=50, textvariable=self.x).grid(
      row=9, column=2, sticky=(W))
    Entry(frame, width=50, textvariable=self.y).grid(
      row=10, column=2, sticky=(W))

  def sign_clicked(self):
    print 'Sign Clicked'
    self.logo.configure(image=self.thiimg)
    self.logo.update()
    
    if self.mode.get() == 'SOOYOUNG - Separate File':
      result = base.CreateSigInSeparateFile(\
          self.input_txt.get(), self.autograph_txt.get(),\
          self.p.get(), self.g.get(), self.x.get())
    
    if self.mode.get() == 'TAEYEON - In The Same File':
      result = base.CreateSigInSameFile(self.input_txt.get(),\
          self.p.get(), self.g.get(), self.x.get())
    
    self.gen_txt.set(result)
    self.logo.configure(image=self.donimg)

  def verify_clicked(self):
    print 'Verify Clicked'
    self.logo.configure(image=self.thiimg)
    self.logo.update()
    
    if self.mode.get() == 'SOOYOUNG - Separate File':
      result = base.VerifySignatureFromFile(\
          self.input_txt.get(), self.autograph_txt.get(), self.p.get(), self.g.get(), self.y.get())
    
    if self.mode.get() == 'TAEYEON - In The Same File':
      result = base.VerifySignatureInTheSameFile(self.input_txt.get(), self.p.get(), self.g.get(), self.y.get())
    
    if result:
      self.logo.configure(image=self.corimg)
    else:
      self.logo.configure(image=self.wroimg)

  def ganti(self):
    if self.mode.get() == 'SOOYOUNG - Separate File':
      self.mode.set('TAEYEON - In The Same File')
      self.logo.configure(image=self.sigimg)
    else:
      self.mode.set('SOOYOUNG - Separate File')
      self.logo.configure(image=self.sepimg)
      
if __name__ == '__main__':
  root = Tk()
  mg = GUI(root)
  root.mainloop();